package com.durian.base.frame;

import android.app.Activity;
import com.durian.base.utils.StringUtils;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Stack;

/**
 * 说明：ActivityStack
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-17 17:34
 * <p/>
 * 版本：version 1.0
 */
public class ActivityStack {
    private static       Stack<WeakReference<Activity>> activities;
    private static final ActivityStack                  instance = new ActivityStack();

    private Activity mActivity;
    private int activityVisibleCount = 0;

    private ActivityStack(){}

    public static ActivityStack create(){
        return instance;
    }

    public void setCurrentActivity(Activity activity){
        if (activity == null){
            activityVisibleCount--;
        }else {
            this.mActivity = activity;
            activityVisibleCount++;
        }
    }

    public Activity getCurrentActivity(){
        return mActivity;
    }

    public boolean isAppClose(){
        return getCount() == 0;
    }

    /**
     * 说明：获取当前Activity栈中数量
     * @return
     */
    public int getCount(){
        int count = 0;
        if (activities != null) {
            count = activities.size();
        }
        return count;
    }

    public boolean isAppVisible(){
        return activityVisibleCount != 0;
    }

    /**
     * 说明：将activity添加到栈中
     * @param activity
     */
    public synchronized void addActivity(Activity activity){
        if (activities == null) {
            activities = new Stack<>();
        }
        activities.add(new WeakReference<>(activity));
    }

    /**
     * 说明：检查是否存在该Activity
     * @param cls
     * @return
     */
    public boolean isCreateActivity(Class<?> cls){
        return findActivity(cls) != null;
    }

    /**
     * 说明：查找activity,没有则返回null
     * @param cls
     * @return
     */
    public Activity findActivity(Class<?> cls){
        Activity activity = null;
        for (WeakReference<Activity> aty : activities) {
            if (isActivityEquals(aty,cls)) {
                activity = aty.get();
                break;
            }
        }
        return activity;
    }

    private boolean isActivityEquals(WeakReference<Activity> weakReference,Class<?> cls){
        return weakReference != null && weakReference.get() != null && StringUtils.isEquals(weakReference.get().getClass().getCanonicalName(),cls.getCanonicalName());
    }

    /**
     * 说明：移除指定activity
     * @param activity
     */
    public synchronized void removeActivity(Activity activity){
        if (activity != null && activities != null && !activities.isEmpty()) {
            Iterator<WeakReference<Activity>> iterator = activities.iterator();
            while (iterator.hasNext()){
                WeakReference<Activity> aty = iterator.next();
                if (aty != null && aty.get() != null && aty.get().equals(activity)){
                    iterator.remove();
                    break;
                }
            }
        }
    }

    /**
     * 说明：结束指定activity
     * @param activity
     */
    public synchronized void finishActivity(Activity activity){
        if (activity != null && activities != null && !activities.isEmpty()) {
            Iterator<WeakReference<Activity>> iterator = activities.iterator();
            while (iterator.hasNext()){
                WeakReference<Activity> aty = iterator.next();
                if (aty != null && aty.get() != null && aty.get().equals(activity)){
                    if (!aty.get().isFinishing()){
                        aty.get().finish();
                    }
                    iterator.remove();
                    break;
                }
            }
        }
    }

    /**
     * 说明：结束指定activity
     * @param cls
     */
    public void finishActivity(Class<?> cls){
        if (cls != null && activities != null && !activities.isEmpty()) {
            Iterator<WeakReference<Activity>> iterator = activities.iterator();
            while (iterator.hasNext()){
                WeakReference<Activity> aty = iterator.next();
                if (isActivityEquals(aty,cls)){
                    if (!aty.get().isFinishing()){
                        aty.get().finish();
                    }
                    iterator.remove();
                }
            }
        }
    }

    /**
     * 说明：清除cls外其他所有activity
     * @param cls
     */
    public void finishOtherActivity(Class<?> cls){
        if (cls != null && activities != null && !activities.isEmpty()) {
            Iterator<WeakReference<Activity>> iterator = activities.iterator();
            while (iterator.hasNext()){
                WeakReference<Activity> aty = iterator.next();
                if (!isActivityEquals(aty,cls)){
                    if (!aty.get().isFinishing()){
                        aty.get().finish();
                    }
                    iterator.remove();
                }
            }
        }
    }

    /**
     * 说明：结束所有activity
     */
    public void finishAllActivity(){
        if (activities != null && !activities.isEmpty()) {
            Iterator<WeakReference<Activity>> iterator = activities.iterator();
            while (iterator.hasNext()){
                WeakReference<Activity> aty = iterator.next();
                if (aty != null && aty.get() != null){
                    if (!aty.get().isFinishing()){
                        aty.get().finish();
                    }
                }
                iterator.remove();
            }
            activities.clear();
        }
    }

    /**
     * 说明：退出应用
     */
    public void AppExit(){
        try {
            finishAllActivity();
            mActivity = null;
        } catch (Exception e) {
            e.printStackTrace();
            Runtime.getRuntime().exit(-1);
        }
    }
}
